package frontend.pages.admin.workdetail;

import backend.model.admin.Workplace;
import backend.service.admin.WorkDetailService;
import backend.service.admin.WorkplaceService;
import frontend.core.TimeTrackingWebApplication;
import frontend.model.admin.WorkDetailUIModel;
import frontend.pages.admin.base.AbstractManagingForm;
import frontend.components.base.ComponentFactory;
import frontend.components.other.DBModelChoiceRenderer;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.List;

public class WorkDetailForm extends AbstractManagingForm<WorkDetailUIModel> {

    private static final String RESOURCE_KEY_FIELD_LABEL_DESCRIPTION = "pages.admin.workDetail.form.description";
    private static final String RESOURCE_KEY_FIELD_LABEL_WORKPLACE = "pages.admin.workDetail.form.workplace";
    private static final String RESOURCE_KEY_FIELD_LABEL_IS_ACTIVE = "pages.admin.workDetail.form.isActive";
    private static final String RESOURCE_KEY_VALIDATION_SAME_DESCRIPTION_AND_WORKPLACE = "validation.form.workDetail.sameWorkDetailDescriptionAndWorkplace";

	public WorkDetailForm(String id, WorkDetailUIModel workDetailUIModel) {
		super(id, workDetailUIModel);		
	}

	@Override
	protected void constructFormContent() {
        Panel descriptionComponent = ComponentFactory.createTextFieldComponent("description", RESOURCE_KEY_FIELD_LABEL_DESCRIPTION, uiModel);
        Panel workplaceComponent = ComponentFactory.createDropDownChoiceComponent("workplace", RESOURCE_KEY_FIELD_LABEL_WORKPLACE,
                uiModel, getActiveWorkplaces(), new DBModelChoiceRenderer<Workplace>(), false);
        Panel isActiveComponent = ComponentFactory.createCheckboxComponent("isActive", RESOURCE_KEY_FIELD_LABEL_IS_ACTIVE, uiModel);

		add(workplaceComponent, descriptionComponent, isActiveComponent);
	}

    private List<Workplace> getActiveWorkplaces() {
        WorkplaceService workplaceService = TimeTrackingWebApplication.getService(WorkplaceService.class);
        return workplaceService.findAllActiveWorkplaces();
    }

    @Override
    protected Class getResponsePage() {
        return WorkDetailManagingPage.class;
    }

    @Override
    protected void doFormValidation() {
        validateNotEmpty(uiModel.getDescription(), RESOURCE_KEY_FIELD_LABEL_DESCRIPTION);
        validateNotEmpty(uiModel.getWorkplace(), RESOURCE_KEY_FIELD_LABEL_WORKPLACE);
        if ((uiModel.getDescription() != null && uiModel.getWorkplace() !=null) && workplaceOrDescriptionWasChanged()) {
            WorkDetailService service =  TimeTrackingWebApplication.getService(WorkDetailService.class);
            if (service.loadWorkDetailByDescriptionAndWorkplace(uiModel.getDescription(), uiModel.getWorkplace()) != null) {
                addGeneralValidationError(RESOURCE_KEY_VALIDATION_SAME_DESCRIPTION_AND_WORKPLACE);
            }
        }
    }

    private boolean workplaceOrDescriptionWasChanged() {
        return false == uiModel.getDescription().equals(uiModel.getDBModel().getDescription()) ||
               false == uiModel.getWorkplace().equals(uiModel.getDBModel().getWorkplace());
    }
}
